<?php

namespace app\admin\controller;

use app\models\{Storehouse, Suser};
use think\Db;
use app\facade\Redis;
use think\Validate;

class Shose extends Base
{

    public function list()
    {
        $model = new Storehouse();
        $data = $this->request->get();
        $results = $model->order('b.id desc')->alias('b');
        if (!empty($data['user_id'])) {
            $results = $results->where('b.user_id', $data['user_id']);
        }
        if (!empty($data['status'])) {
            $results = $results->where('b.status', $data['status']);
        }
        if (!empty($data['paystatus'])) {
            $results = $results->where('b.paystatus', $data['paystatus']);
        }
        if (!empty($data['cangku'])) {
            $results = $results->where('b.type', $data['cangku']);
        }
        if (!empty($data['types'])) {
            $results = $results->where('b.types', $data['types']);
        }
        if (!empty($data['starttime'])) {
            $data['starttime'] = date('Y-m-d', strtotime($data['starttime'])) . ' 00:00:00';
            $endtime = !empty($data['endtime']) ? date('Y-m-d', strtotime($data['endtime'])) . ' 23:59:59' : date('Y-m-d H:i:s');
            $results = $results->whereBetweenTime('b.times', $data['starttime'], $endtime);
        }
        $results = $results->field('b.*');
        // a仓库
        if (!empty($this->request->admin->type)) {
            $results = $results->where(['b.type' => $this->request->admin->type]);
        }
        $bdata['rtotal'] = $results->sum('rtotal');
        $bdata['rnumber'] = $results->sum('rnumber');
        $bdata['ctotal'] = $results->sum('ctotal');
        $bdata['cnumber'] = $results->sum('cnumber');
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['status'] = config()["config"]['status'][$result['status']] ?? '';
            $result['paystatus'] = config()["config"]['paystatus'][$result['paystatus']] ?? '';
            $result['cangku'] = config()["config"]['cangku'][$result['type']] ?? '';
            $result['type'] = config()["config"]['rktype'][$result['types']] ?? '';
        }
        $status = config()["config"]['status'] ?? [];
        $paystatus = config()["config"]['paystatus'] ?? [];
        $cangku = config()["config"]['cangku'] ?? [];
        $types = config()["config"]['rktype'] ?? [];
        return view(null, [
            'list' => $results,
            'statuss' => $status,
            'paystatuss' => $paystatus,
            'cangku' => $cangku,
            'types' => $types,
            'bdata' => $bdata
        ]);
    }

    public function list_edit()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '提交频繁,请' . $rdslist . 's后再提交']);
            }
            Redis::setex('clickreturn', 1, 1);
            $data = $this->request->post();
            $data['admin_id'] = $this->request->admin->id;
            $model = new Storehouse();
            if (!empty($this->request->admin->type)) {
                $data['type'] = $this->request->admin->type;
            } else {
                if (empty($data['type'])) {
                    return json(['status' => 1, 'msg' =>  '客户组别不能为空']);
                }
            }
            if (!empty($data['id'])) {
                $res = $model->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $data['admin_id'] = $this->request->admin->id;
                $res = $model->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
            }
            return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
        } else {
            $model = new Storehouse();
            $id = $this->request->get('id/d', 0);
            $row = $model->where('id', $id)->find();
            $types = config()["config"]['rktype'] ?? [];
            $status = config()["config"]['status'] ?? [];
            $paystatus = config()["config"]['paystatus'] ?? [];
            $cangku = config()["config"]['cangku'] ?? [];
            return view('', [
                'row' => $row,
                'types' => $types,
                'statuss' => $status,
                'paystatuss' => $paystatus,
                'cangku' => $cangku,
                'admin' => $this->request->admin
            ]);
        }
    }
}
